/*
 * TcpClient_test.cpp
 *
 * @build   make evpp
 * @server  bin/TcpServer_test 1234
 * @client  bin/TcpClient_test 1234
 *
 */

#include <iostream>

#include "TcpClient.h"
#include "htime.h"

#define TEST_RECONNECT 1
#define TEST_TLS 0

using namespace hv;

int test_tcp_hv(void)
{
    int remote_port = 88;
    const char *remote_host = "192.168.120.98"; //"127.0.0.1";
    TcpClient cli;
    int connfd = cli.createsocket(remote_port, remote_host);
    if (connfd < 0)
    {
        return -20;
    }
    printf("client connect to port %d, connfd=%d ...\n", remote_port, connfd);
    std::string aa = "hello from hv tcp client.";
    std::cout << ">> " << aa << std::endl;
    cli.send(aa);
    cli.onConnection = [&cli](const SocketChannelPtr &channel)
    {
        std::string peeraddr = channel->peeraddr();
        if (channel->isConnected())
        {
            printf("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
            // send(time) every 3s
            setInterval(3000, [channel](TimerID timerID)
                        {
                if (channel->isConnected()) {
                    if (channel->isWriteComplete()) {
                        char str[DATETIME_FMT_BUFLEN] = {0};
                        datetime_t dt = datetime_now();
                        datetime_fmt(&dt, str);
                        channel->write(str);
                    }
                } else {
                    killTimer(timerID);
                } });
        }
        else
        {
            printf("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
        }
        if (cli.isReconnect())
        {
            printf("reconnect cnt=%d, delay=%d\n", cli.reconn_setting->cur_retry_cnt, cli.reconn_setting->cur_delay);
        }
    };
    cli.onMessage = [](const SocketChannelPtr &channel, Buffer *buf)
    {
        printf("< %.*s\n", (int)buf->size(), (char *)buf->data());
    };

#if TEST_RECONNECT
    // reconnect: 1,2,4,8,10,10,10...
    reconn_setting_t reconn;
    reconn_setting_init(&reconn);
    reconn.min_delay = 1000;
    reconn.max_delay = 10000;
    reconn.delay_policy = 2;
    cli.setReconnect(&reconn);
#endif

#if TEST_TLS
    cli.withTLS();
#endif

    std::cout << "开始连接" << std::endl;
    std::string str = "hello from hv tcp client.";
    cli.start();
    sleep(300);
    cli.stop();

    return 0;
}
